//
//  GXNewfeatureViewController.m
//  SXH
//
//  Created by 龚啸 on 15/9/23.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "GXNewfeatureViewController.h"
#import "AdViewController.h"
#import "MainTbaBarController.h"
#import "AppDelegate.h"

#define HWNewfeatureCount 4

@interface GXNewfeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;

@property (nonatomic, weak) UIScrollView *scrollView;
@end

@implementation GXNewfeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    // 1.创建一个scrollView：显示所有的新特性图片
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    // 2.添加图片到scrollView中
    CGFloat scrollW = scrollView.frame.size.width;
    CGFloat scrollH = scrollView.frame.size.height;
    for (int i = 0; i<HWNewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        
        imageView.frame = CGRectMake(i * scrollW, -20, scrollW, scrollH);
        // 显示图片
        NSString *name = [NSString stringWithFormat:@"引导页%d.png", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        
        // 如果是最后一个imageView，就往里面添加其他内容
        if (i == HWNewfeatureCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
    
#warning 默认情况下，scrollView一创建出来，它里面可能就存在一些子控件了
#warning 就算不主动添加子控件到scrollView中，scrollView内部还是可能会有一些子控件
    
    // 3.设置scrollView的其他属性
    // 如果想要某个方向上不能滚动，那么这个方向对应的尺寸数值传0即可
    scrollView.contentSize = CGSizeMake(HWNewfeatureCount * scrollW, 0);
    scrollView.bounces = NO; // 去除弹簧效果
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    
    // 4.添加pageControl：分页，展示目前看的是第几页
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = HWNewfeatureCount;
    pageControl.backgroundColor = [UIColor redColor];
    pageControl.currentPageIndicatorTintColor = RGBCOLOR(253, 98, 42);
    pageControl.pageIndicatorTintColor = RGBCOLOR(189, 189, 189);
    
    pageControl.center =CGPointMake(scrollW * 0.5, scrollH - 50);
  
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
    
    
    UIButton *btn =[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-30, self.view.frame.size.height-30, 30, 30)];
    [btn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
//    [btn setBackgroundColor:[UIColor blackColor]];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
        double page = scrollView.contentOffset.x / scrollView.frame.size.width;
        // 四舍五入计算出页码
        self.pageControl.currentPage = (int)(page + 0.5);
        // 1.3四舍五入 1.3 + 0.5 = 1.8 强转为整数(int)1.8= 1
        // 1.5四舍五入 1.5 + 0.5 = 2.0 强转为整数(int)2.0= 2
        // 1.6四舍五入 1.6 + 0.5 = 2.1 强转为整数(int)2.1= 2
        // 0.7四舍五入 0.7 + 0.5 = 1.2 强转为整数(int)1.2= 1
}
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    

    // 2.开始微博
    UIButton *startBtn = [[UIButton alloc] init];
    
    startBtn.frame = CGRectMake(SCREEN_WIDTH * 95/320,SCREEN_HEIGHT * 352/568, SCREEN_WIDTH*130/320 ,SCREEN_HEIGHT * 40/568) ;
//    startBtn.backgroundColor = [UIColor blackColor];
    
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}

- (void)shareClick:(UIButton *)shareBtn
{
    // 状态取反
    shareBtn.selected = !shareBtn.isSelected;
}

- (void)startClick
{
    // 切换到HWTabBarController
    /*
     切换控制器的手段
     1.push：依赖于UINavigationController，控制器的切换是可逆的，比如A切换到B，B又可以回到A
     2.modal：控制器的切换是可逆的，比如A切换到B，B又可以回到A
     3.切换window的rootViewController
     */
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    UINavigationController *nav = [[UINavigationController alloc]init];
//    
//    
//
//    WrapperMainViewController *wrapperMainVC = [[WrapperMainViewController alloc]init];
//    [nav addChildViewController:wrapperMainVC];
//    [wrapperMainVC.navigationController setNavigationBarHidden:YES];
//        
//   
//
//    window.rootViewController = nav;
    
    MainTbaBarController *vc = [[MainTbaBarController alloc] init];
    [[AppDelegate instance].window changeRootViewController:vc];
    
    // modal方式，不建议采取：新特性控制器不会销毁
    //    HWTabBarViewController *main = [[HWTabBarViewController alloc] init];
    //    [self presentViewController:main animated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"GXNewfeatureViewController"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"GXNewfeatureViewController"];
}

@end
